package com.ruoyi.workflow.config;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import vip.lsjscl.flowboot.util.UserInfoProvider;
import vip.lsjscl.flowboot.util.model.User;

import java.util.List;

/**
 * 自定义用户信息提供程序
 *
 * @author zhangxingju
 * @date 2025/02/15
 */
@Component
@RequiredArgsConstructor
public class CustomUserInfoProvider implements UserInfoProvider {

    private final ISysUserService userService;

    @Override
    public String getCurrentUserId() {
        // 实现获取当前用户ID的逻辑
        // 例如从 Session、Token 或者其他地方获取
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return loginUser.getUserId().toString();
    }

    @Override
    public String getCurrentUsername() {
        // 实现获取当前用户名的逻辑
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return loginUser.getUsername();
    }

    /**
     * 按用户 ID 获取用户列表
     *
     * @return {@link List }<{@link User }>
     */
    @Override
    public List<User> getUserListByUserIds(List<String> userIds) {
        List<SysUser> sysUsers = userService.selectUserByIds(userIds);
        return sysUsers.stream().map(sysUser -> {
            User user = new User();
            user.setUserName(sysUser.getUserName());
            user.setNickName(sysUser.getNickName());
            user.setUserId(sysUser.getUserId().toString());
            return user;
        }).toList();
    }

    /**
     * 获取当前用户部门id
     *
     * @return {@link List }<{@link User }>
     */
    @Override
    public String getCurrentDeptId() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return loginUser.getDeptId().toString();
    }
}